﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SF.Base.Core
{
	public static class RegexHelper
	{
		public struct RegStr
		{
			public const string Hyperlink = "<a.*?href=\"(?<Href>.*?)\".*?>(?<Text>[^<].*?)</a>";
		}

		public static MatchCollection GetRegValue(string HtmlCode, string RegexString)
		{
			return GetRegValue(HtmlCode, RegexString,false);
		}

		public static MatchCollection GetRegValue(string HtmlCode, string RegexString, bool RightToLeft)
		{
			Regex r;
			if (RightToLeft == true)
			{
				r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
			}
			else
			{
				r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
			}
			return r.Matches(HtmlCode);
		}


		/// <summary>
		/// 正则表达式取值
		/// </summary>
		/// <param name="HtmlCode">源码</param>
		/// <param name="RegexString">正则表达式</param>
		/// <param name="GroupKey">正则表达式分组关键字</param>
		/// <returns></returns>
		public static string[] GetRegValue(string HtmlCode, string RegexString, string GroupKey)
		{			
			return GetRegValue(HtmlCode , RegexString,GroupKey,true);
		}

		/// <summary>
        /// 正则表达式取值
        /// </summary>
        /// <param name="HtmlCode">源码</param>
        /// <param name="RegexString">正则表达式</param>
        /// <param name="GroupKey">正则表达式分组关键字</param>
        /// <param name="RightToLeft">是否从右到左</param>
        /// <returns></returns>
		public static string[] GetRegValue(string HtmlCode, string RegexString, string GroupKey, bool RightToLeft)
        {
            MatchCollection m;
            Regex r;
            if (RightToLeft == true)
            {
                r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
            }
            else
            {
                r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            }

            m= r.Matches(HtmlCode);
            string[] MatchValue = new string[m.Count];
            for (int i = 0; i < m.Count; i++)
            {
                MatchValue[i] = m[i].Groups[GroupKey].Value;
            }
            return MatchValue;
        }

		#region 字符串验证

		#region 判断字符串是否为整型
		/**/
		/// <summary>  
		/// 判断字符串是否为整型  
		/// </summary>  
		/// <param name="source"></param>  
		/// <returns></returns>  
		#endregion
		public static bool IsInteger(string source) {
			if (source == null || source == "") {
				return false;
			}

			if (Regex.IsMatch(source, "^((\\+)\\d)?\\d*$")) {
				return true;
			}
			else {
				return false;
			}
		}


		#region Email 格式是否合法
		/**/
		/// <summary>  
		/// Email 格式是否合法  
		/// </summary>  
		/// <param name="strEmail"></param>  
		#endregion
		public static bool IsEmail(string strEmail) {
			return Regex.IsMatch(strEmail, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
		}


		#region 判断是否IP
		/**/
		/// <summary>  
		/// 判断是否IP  
		/// </summary>  
		/// <param name="source"></param>  
		/// <returns></returns>  
		#endregion
		public static bool IsIP(string source) {
			return Regex.IsMatch(source, @"^(((25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]|1[0-6][0-9]|1[1-9]|[2-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(192\.(25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0-9]|[0-9]))|(172\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9]|[4-9][0-9]|[0-9])))\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
		}


		#region 检查字符串是否为A-Z、0-9及下划线以内的字符

		/**/
		/// <summary>  
		/// 检查字符串是否为A-Z、0-9及下划线以内的字符  
		/// </summary>  
		/// <param name="str">被检查的字符串</param>  
		/// <returns>是否有特殊字符</returns>  
		#endregion
		public static bool IsLetterOrNumber(string str) {
			bool b = System.Text.RegularExpressions.Regex.IsMatch(str, "[a-zA-Z0-9_]");
			return b;
		}


		#region 验输入字符串是否含有“/\<>:.?*|$]”特殊字符
		/**/
		/// <summary>  
		/// 验输入字符串是否含有“/\:.?*|$]”特殊字符  
		/// </summary>  
		/// <param name="source"></param>  
		/// <returns></returns>  
		#endregion
		public static bool IsSpecialChar(string source) {
			Regex r = new Regex(@"[/\<>:.?*|$]");
			return r.IsMatch(source);
		}


		#region 是否全为中文/日文/韩文字符
		/**/
		/// <summary>  
		/// 是否全为中文/日文/韩文字符  
		/// </summary>  
		/// <param name="source">源字符串</param>  
		/// <returns></returns>  
		#endregion
		public static bool IsChineseChar(string source) {
			//中文/日文/韩文: [\u4E00-\u9FA5]  
			//英文:[a-zA-Z]  
			return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
		}


		#region 是否包含双字节字符(允许有单字节字符)
		/**/
		/// <summary>  
		/// 是否包含双字节字符(允许有单字节字符)  
		/// </summary>  
		/// <param name="source"></param>  
		/// <returns></returns>  
		#endregion
		public static bool IsDoubleChar(string source) {
			return Regex.IsMatch(source, @"[^\x00-\xff]");
		}


		#region 是否为日期型字符串
		/**/
		/// <summary>  
		/// 是否为日期型字符串  
		/// </summary>  
		/// <param name="source">日期字符串(2005-6-30)</param>  
		/// <returns></returns>  
		#endregion
		public static bool IsDate(string source) {
			return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
		}


		#region 是否为时间型字符串
		/**/
		/// <summary>  
		/// 是否为时间型字符串  
		/// </summary>  
		/// <param name="source">时间字符串(15:00:00)</param>  
		/// <returns></returns>  
		#endregion
		public static bool IsTime(string source) {
			return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
		}


		#region 是否为日期+时间型字符串
		/**/
		/// <summary>  
		/// 是否为日期+时间型字符串  
		/// </summary>  
		/// <param name="source"></param>  
		/// <returns></returns>  
		#endregion
		public static bool IsDateTime(string source) {
			return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) ((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
		}


		#region 是否为钱币型数据
		/**/
		/// <summary>  
		/// 是否为钱币型数据  
		/// </summary>  
		/// <param name="source"></param>  
		/// <returns></returns>  
		#endregion
		public static bool IsMoney(string source) {
			return false;
		}


		public static bool IsQQ(string source) {
			return Regex.IsMatch(source, @"^[1-9][0-9]{4,9}$");
		}

		#endregion
	}
}
